<?php
ArrowController::importModel("org.arrowplatform.controls.forms.ctrl.ArrowForm");
ArrowController::importModel("org.arrowplatform.controls.forms.ArrowFormException");

/**
 * Class Persistent 
 *
 * FormManager provides API to manage forms
 * It's used by ArrowTemplateParser and by user code 
 *
 * @author Artur Kmera 
 * @version 0.9
 * @package Arrow
 * @subpackage Forms
 */
class ArrowFormCtrlManager implements IArrowCacheable, IArrowControlsManager{

	
	/**
	 * Constant with conf file
	 */
	//const CONF_FILE = "/conf/forms-conf.xml";
	

	/**
	 * Array with registred fields 
	 * where key is register name and value is field model
	 *
	 * @var array
	 */
	private $registredFields = array(
			"text" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormText",
			"number" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormNumber",
			"radiogroup" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormRadioGroup",
			"checkbox" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormCheckbox",
			"checkboxgroup" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormCheckboxGroup",
			"textarea" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormTextarea",
			"password" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormPassword",
			"mail" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormMail",
			"hidden" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormHidden",
			"file" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormFile",
			"select" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormSelect",
			"option" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormOption",
			"imageupload" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormImageupload",
      		"submit" => "org.arrowplatform.controls.forms.ctrl.simple.ArrowFormSubmit",
      		"error" => "org.arrowplatform.controls.forms.ctrl.ArrowFormError",
			"errorbound" => "org.arrowplatform.controls.forms.ctrl.ArrowFormErrorBound",
			"error" => "org.arrowplatform.controls.forms.ctrl.ArrowFormError",
			"date" => "org.arrowplatform.controls.forms.ctrl.javascript.ArrowFormDate",
			"wyswig" => "org.arrowplatform.controls.forms.ctrl.javascript.ArrowFormWyswig",
			"slidebar" => "org.arrowplatform.controls.forms.ctrl.javascript.ArrowFormSlideBar",
			"switch" => "org.arrowplatform.controls.forms.ctrl.javascript.ArrowFormSwitch",
			"code" => "org.arrowplatform.controls.forms.ctrl.javascript.ArrowFormCode"
		);
	
	
	/**
	 * Singleton static var
	 *
	 * @var ArrowFieldsManager
	 */
	private static $selfInstance = null;
	
	/**
	 * Singleton
	 * 
	 * @return ArrowFieldsManager
	 */
	public static function getDefault( ) {
		if (self :: $selfInstance == null ) {
			self :: $selfInstance = new ArrowFormCtrlManager();
		}
		return self :: $selfInstance;
	}
	
	/**
	 * Constructor loads external data from ArrowView
	 *
	 */
	private function __construct(){
		
		//$confFile = ArrowController::$activeProject->getPath()."/".self::CONF_FILE;
		//$cacheFile = ArrowCacheProvider::getFileCache( $this, $confFile, array( "file_prefix" => ArrowController::$activeProject->getId()."_forms_config" )   );
		//require_once $cacheFile;
		//$this->registredFields = array_merge( $this->registredFields, $cache["fields"]);
	}
	
	public function getRegistredFields(){
		return $this->registredFields;	
	}
	
	/**
	 * IArrowCacheable implementation
	 *
	 * @param Array $params
	 * @return Array
	 */
	public function generateCache($params){
		$tmp = array();
		$conf = ArrowController::$activeProject->getXMLConfig(self::CONF_FILE);
		$tmp["fields"] = array();
		foreach( $conf->formFields->formField as $field ){
			$tmp["fields"][(string) $field["name"]] = (string) $field["model"];
		}
		return $tmp;
	}

	
	/**
	 * Provides functionality to generate HTML fields.
	 * Used by ArrowTemplateParser
	 *
	 * @param array $params params from tag attributes
	 * @param array $tagIn detected tag tags 
	 * @return string
	 */
	public function fieldHandle(  $params, $prefix, $name, $class ){
  
    	if( !isset($this->registredFields[$name] ) ) {
	        throw new ArrowFormException( "[ArrowFormCtrlManager::fieldHandle] Brak zarejestrowanego pola: {$name}" , 4 ) ;
	    }
	    
		$model = $this->registredFields[$name];
		$class = explode(".", $model);
		$class = array_pop($class);
		ArrowController::importModel($this->registredFields[$name]);
		
		$fieldObj = new $class( $params, $prefix, $name,  ArrowView::getCurrentView()->getTemplateDescriptor()->getKey(), $class );
		
		return array( "model" => $model, "object"=>$fieldObj );
	}

	
	/**
	 * Registers new field
	 *
	 * @param string $parserName
	 * @param string $model
	 */
	public function registerField( $parserName, $model ){
		$this->registerField[$parserName] = $model;
	}
	
	
	public function getControlVersionsConf($prefix,$tag){
		$tmp = array();
		foreach($this->registredFields as $param => $action){
			$obj = $this->fieldHandle( array(), $prefix, $param, ""  );
			$obj["object"]->configure();
			$params =  $obj["object"]->getProperties();
			$tmp[$param] = array(
				"params" => $params,
				"requiredParams" => $obj["object"]->getRequiredProperties()
			); 
		}
		return $tmp;
	}	
	
}
?>